// var baseurl = "http://localhost:8080/admin/selectadminaftersale"
//
// var rque = "?aftsaleSolve=" + aftsaleSolve
//
// url: baseurl + rquelve=" + aftsaleSolve
//
// url: 'http://localhost:8080/admin/selectadminaftersale?aftsaleSolve=' + aftsaleSolve + '&afterSaleId=' + afterSaleId + '&'

layui.use('table', function () {
    var table = layui.table;
    table.render({
        elem: '#test'
        , url: 'http://localhost:8080/admin/selectadminaftersale'
        , id: 'aftersaleId'
        , toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
        , title: '用户数据表'
        , cols: [[
            {type: 'checkbox', fixed: 'left'}
            , {field: 'goodPhoto', title: '商品图片', templet: '#img', width: 100}
            , {field: 'aftersaleId', title: '售后单号', width: 100}
            , {field: 'orderId', title: '订单单号', width: 100}
            , {field: 'userId', title: '用户ID', width: 90}
            , {field: 'userName', title: '用户昵称', width: 100}
            , {field: 'userPhone', title: '用户电话', width: 100}
            , {field: 'aftersaleRequi', title: '售后要求', width: 90}
            , {field: 'aftersaleSolve', title: '售后状态', width: 100}
            , {field: 'orderState', title: '订单状态', width: 110}
            , {field: 'orderCount', title: '数量', width: 80, sort: true}
            , {field: 'orderMoney', title: '总价', width: 80, sort: true}
            , {field: 'goodName', title: '商品名称', width: 100}
            , {fixed: 'right', title: '操作', toolbar: '#barDemo', width: 150}
        ]]
        , page: { //支持传入 laypage 组件的所有参数（某些参数除外，如：jump/elem） - 详见文档
            layout: ['prev', 'page', 'next', 'skip', 'count'] //自定义分页布局
            // ,groups: 3 //只显示 1 个连续页码
            // ,first: '首页' //不显示首页
            // ,last: '尾页' //不显示尾页
            , theme: '#7185a2'
        }
    });


    //头工具栏事件
    table.on('toolbar(test)', function (obj) {
        var checkStatus = table.checkStatus(obj.config.id);
        switch (obj.event) {
            case 'getCheckLength':
                var data = checkStatus.data;
                layer.msg('选中了：' + data.length + ' 个');
                break;
            case 'isAll':
                layer.msg(checkStatus.isAll ? '全选' : '未全选');
                break;
        }
    });

    //监听行工具事件
    table.on('tool(test)', function (obj) {
        var data = obj.data;
        if (obj.event === 'del') {
            layer.confirm('真的删除行么', function (index) {
                obj.del();
                console.log("删除按钮点击 -> " + obj.data.aftersaleId)
                dlevalue(obj.data.aftersaleId)
                layer.close(index);
            });
        } else if (obj.event === 'edit') {
            // 如果input标签里值为空则赋值为""，否则返回为本值
            let aftsasolve = inputvalue(obj.data.aftersaleSolve)
            let aftsareq = inputvalue(obj.data.aftersaleRequi)

            localStorage.setItem("aftersaleSolve", aftsasolve)
            localStorage.setItem("aftersaleRequi", aftsareq)
            layer.alert(
                '<form class="layui-form layui-form-pane" action="">\n' +
                '    <div class="layui-form-item">\n' +
                '        <label class="layui-form-label">售后状态</label>\n' +
                '        <div class="layui-input-block">\n' +
                '            <input type="text" id="aftsalestate" name="aftsalestate" lay-verify="title" autocomplete="off" placeholder="请输入 已处理 或 未处理" class="layui-input">\n' +
                '        </div>\n' +
                '        <p id="aftsalestatecon" style="color: #b1191a"></p>\n' +
                '        <label class="layui-form-label" style="margin-top: 40px">售后要求</label>\n' +
                '        <div class="layui-input-block" style="margin-top: 40px">\n' +
                '            <input type="text" id="aftsalerequt" name="aftsalerequt" lay-verify="title" autocomplete="off" placeholder="请输入 退货 或 换货 或 退款" class="layui-input">\n' +
                '        </div>\n' +
                '        <p id="aftsalerequtcon" style="color: #b1191a"></p>\n' +
                '    </div>\n' +
                '</form>\n' +
                '<script>\n' +
                '    $("#aftsalestate").val(localStorage.getItem("aftersaleSolve"))\n' +
                '    $("#aftsalerequt").val(localStorage.getItem("aftersaleRequi"))\n' +
                '</script>',
                {
                    title: '修改表单',
                    type: 0,
                    skin: 'layui-layer-molv', //样式类名
                    closeBtn: 1, //显示关闭按钮
                    anim: 0,  // 动画
                    shadeClose: true, //开启遮罩关闭
                    area: ['380px', '300px'],
                }, function () {
                    // 售后状态点击确定并未填数值
                    // let a = document.getElementById('aftsalestate').value
                    aftsalestatejudge($("[name='aftsalestate']").val())
                    // 售后状态填值并移动聚焦
                    $("#aftsalestate").blur(function () {
                        aftsalestatejudge($(this).val())
                    })

                    //  售后要求未填值并提交
                    aftsalerequtjudge($("[name='aftsalerequt']").val())
                    // 售后要求填值并移动聚焦
                    $("#aftsalerequt").blur(function () {
                        aftsalerequtjudge($(this).val())
                    })

                    //  得到售后ID对数据库进行更改
                    let aftersaId = obj.data.aftersaleId
                    alliskey(aftersaId)

                }
            )
        }
    });
});

function inputvalue(value) {
    if (value === null) {
        return value = ""
    } else {
        return value
    }
}

function dlevalue(deletId) {
    $.getJSON("http://localhost:8080/admin/deleteAdminAftersale", "aftSaleId=" + deletId, function (data) {
        console.log(data.code)
        showResult(data)
    })
}

function aftsalestatejudge(aftsalestateval) {
    if (aftsalestateval === "已处理" || aftsalestateval === "未处理") {
        $("#aftsalestatecon").html("")
        return true
    } else {
        $("#aftsalestatecon").html("输入错误或为空")
        return false
    }
}

function aftsalerequtjudge(aftsalerequtval) {
    if (aftsalerequtval === "退货" || aftsalerequtval === "换货" || aftsalerequtval === "退款") {
        $("#aftsalerequtcon").html("")
        return true
    } else {
        $("#aftsalerequtcon").html("输入错误或为空")
        return false
    }
}

function alliskey(aftersaId) {
    // 获得售后状态
    let aftsalestate = $("#aftsalestate").val()
    // 获得售后要求
    let aftsalerequt = $("#aftsalerequt").val()

    if (aftsalerequtjudge(aftsalerequt) && aftsalestatejudge(aftsalestate)) {
        $.ajax({
            url: "http://localhost:8080/admin/updataAdminAftersale",
            data: "aftSaleId=" + aftersaId + "&aftSaleState=" + aftsalestate + "&aftSaleReq=" + aftsalerequt,
            dataType: "json",
            type: "get",
            success: function (data) {
                showResult(data)
            }
        })
    }
}

function showResult(data) {
    if (data.code === 0) {
        layer.alert('<div style="height: 45px;text-align:center;line-height: 45px">操作成功</div>', {
            title: '提示',
            skin: 'layui-layer-lan'
            , closeBtn: 0
        }, function (index) {
            if (localStorage.getItem("condition") !== $("#aftall").html()) {
                layer.close(index)
                layuireload()
            } else {
                layer.close(index)
                allreload()
            }
        })
    } else {
        layer.alert('<div style="height: 45px;text-align:center;line-height: 45px">操作失败</div>', {
            title: '提示',
            skin: 'layui-layer-lan'
            , closeBtn: 0
        })
    }
}

$("#aftprocessed").click(function () {
    // 已处理
    localStorage.setItem("condition", $("#aftprocessed").html())
    hdcolorno()
    hdcolorclick("#aftprocessed")
    mylayuireload()
})

$("#aftuntreated").click(function () {
    // 未处理
    localStorage.setItem("condition", $("#aftuntreated").html())
    hdcolorno()
    hdcolorclick("#aftuntreated")
    mylayuireload()
})

$("#aftall").click(function () {
    localStorage.setItem("condition", $("#aftall").html())
    hdcolorno()
    hdcolorclick("#aftall")
    myallreload()
})

function mylayuireload() {

    layui.table.reload('aftersaleId', {
        url: 'http://localhost:8080/admin/selectadminaftersale'
        , page: {
            curr: 1
        }
        , where:
            {
                aftsaleSolve: localStorage.getItem("condition")
            }
    })
}

function layuireload() {

    layui.table.reload('aftersaleId', {
        url: 'http://localhost:8080/admin/selectadminaftersale'
        , page: {
            curr: $(".layui-laypage-skip .layui-input").val()
        }
        , where:
            {
                aftsaleSolve: localStorage.getItem("condition")
            }
    }, 'data')
}

function myallreload() {

    layui.table.reload('aftersaleId', {
        url: 'http://localhost:8080/admin/sellayall'
        , page: {
            curr: 1
        }
    })
}

function allreload() {

    layui.table.reload('aftersaleId', {
        url: 'http://localhost:8080/admin/sellayall'
        , page: {
            curr: $(".layui-laypage-skip .layui-input").val()
        }
    })
}

function hdcolorno() {
    $('#aftall').css('color', '')
    $('#aftprocessed').css('color', '')
    $('#aftuntreated').css('color', '')
}

function hdcolorclick(htvalue) {
    $(htvalue).css('color', '#43A2FF')
}

// layui.use('upload', function () {
//     var $ = layui.jquery,
//         upload = layui.upload;
//
//     //指定允许上传的文件类型
//     upload.render({
//         elem: '#test3'
//         , url: "{:url('test/uploadFile')}"
//         , accept: 'file' //普通文件
//         , done: function (res) {
//             // console.log(res)
//             if (res.code == 0) {
//                 layer.msg(res.msg, {icon: 1});
//                 setTimeout(function () {
//                     window.location.href = "{:url('test/index')}"
//                 }, 2000);
//             }
//         }
//     });
// })